package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scs.application.consts.SerialKeys;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.utils.BeanUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.ReagentTeam;
import com.scs.application.modules.base.entity.ReagentTeamUser;
import com.scs.application.modules.base.mapper.ReagentTeamMapper;
import com.scs.application.modules.base.service.ReagentTeamService;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.base.service.ReagentTeamUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 试剂组 服务实现类
 * </p>
 */
@Service
public class ReagentTeamServiceImpl extends BaseServiceImpl<ReagentTeamMapper, ReagentTeam> implements ReagentTeamService {

    @Resource
    private ReagentTeamUserService teamUserService;

    @Override
    public boolean saveOrUpdate(ReagentTeam entity) {
        if (entity.isNewRecord()) {
            if (checkName(entity.getName())) {
                throw new BusinessException("已存在相同的试剂组名称，请重新输入");
            }
            entity.setCode(serialRuleAdapter.getSerialRuleService().generateCode(SerialKeys.BASE_REAGENT_TEAM_CODE));
        } else {
            if (!getById(entity.getId()).getName().equals(entity.getName()) && checkName(entity.getName())) {
                throw new BusinessException("已存在相同的试剂组名称，请重新输入");
            }
        }
        return super.saveOrUpdate(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer syncUser() {
        // 科室人员查询结果
        List<ReagentTeamUser> teamUsers = baseMapper.selectStaff();
        // 现有试剂组人员
        List<ReagentTeamUser> oldUsers = teamUserService.list();
        List<ReagentTeamUser> saveOrUpdateList = new ArrayList<>();
        List<String> deleteList = new ArrayList<>();

        for (ReagentTeamUser t : teamUsers) {
            boolean find = false;
            for (ReagentTeamUser o : oldUsers) {
                if (t.getCode().equals(o.getCode())) {
                    // 更新人员
                    BeanUtils.copyProperties(t, o, "id", "creator", "gmtCreate", "userCode");
                    saveOrUpdateList.add(o);
                    find = true;
                    break;
                }
            }
            if (!find) {
                // 新增人员
                saveOrUpdateList.add(t);
            }
        }

        for (ReagentTeamUser o : oldUsers) {
            boolean find = false;
            for (ReagentTeamUser t : teamUsers) {
                if (o.getCode().equals(t.getCode())) {
                    find = true;
                    break;
                }
            }
            if (!find) {
                // 删除人员
                deleteList.add(o.getId());
            }
        }
        if (saveOrUpdateList.size() > 0) {
            teamUserService.saveOrUpdateBatch(saveOrUpdateList);
        }
        if (deleteList.size() > 0) {
            teamUserService.removeByIds(deleteList);
        }
        return (saveOrUpdateList.size() + deleteList.size());
    }

    @Override
    public List<ReagentTeam> getCurrentTeam() {
        return this.baseMapper.getByStaffId(UserUtils.currentUser().getCode());
    }

    /**
     * 校验检验设备名称是否重复
     */
    private boolean checkName(String name) {
        return getOne(new QueryWrapper<ReagentTeam>().eq("name", name)) != null;
    }

}
